package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import netty.mq.RabbitMQManager;
import netty.websocket.WebSocketServerInitializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@Slf4j
@Component
public class NettyServer {
    @Resource
    private WebSocketServerInitializer webSocketServerInitializer;
    private int port;
    public NettyServer setPort(int port){
        this.port=port;
        return this;
    }
    public void start(){
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workGroup=new NioEventLoopGroup();
        try {
            //构建netty服务器
            ServerBootstrap server=new ServerBootstrap();
            //主从线程池放到服务器中
            server.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class)//设置NIO双向通道
                    .childHandler(webSocketServerInitializer)//设置处理器，用于处理workGroup
            ;
            //启动server，启动方式：同步
            ChannelFuture channelFuture = server.bind(this.port).sync();

            //监听关闭的channel
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
//    public void stop(){
//        this.bossGroup.shutdownGracefully();
//        this.workGroup.shutdownGracefully();
//    }
    public static void main(String[] args) throws InterruptedException {
        RabbitMQManager rabbitMQManager = new RabbitMQManager("localhost",5672,"guest","guest","/");
        //主从线程池
        //定义主线程池，客户端连接
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        //定义从线程池，处理主线程池的任务
        EventLoopGroup workGroup=new NioEventLoopGroup();

        try {
            rabbitMQManager.start();
            //构建netty服务器
            ServerBootstrap server=new ServerBootstrap();
            //主从线程池放到服务器中
            server.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class)//设置NIO双向通道
                    .childHandler(new WebSocketServerInitializer())//设置处理器，用于处理workGroup
            ;
            //启动server，启动方式：同步
            ChannelFuture channelFuture = server.bind(875).sync();

            //监听关闭的channel
            channelFuture.channel().closeFuture().sync();
        } catch (IOException e) {
            log.error("rabbitMQ启动异常");
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
            rabbitMQManager.shutdown();
        }

    }
}
